<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/5/29
 * Time: 10:30
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Message extends Base {
    public function index(){
        if(request()->isAjax()){
            $map = array();
            extract(input());
            if(isset($keyword)&&$keyword!="")
            {
                $map['ut.topic_title|a.topic_message_content|uuu.user_username|uuu.user_phone'] = ['like',"%" . $keyword . "%"];
            }
            if(isset($province)&&$province!="")
            {
                $map['a.topic_status'] = $province;
            }
            if(isset($start) && $start != '' && isset($end) && $end != ''){
                $map['topic_message_time'] = ['between', [$start, $end]];
            }
            if(isset($start) && $start != '' && isset($end) && $end == ''){
                $map['topic_message_time'] = ['> time', $start];
            }
            if(isset($start) && $start == '' && isset($end) && $end != ''){
                $map['topic_message_time'] = ['< time', $end];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('topic_message')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.topic_message_user','left')
                ->join('up_topic ut','ut.topic_id =a.topic_id','left')
                ->where($map)->count();// 获取总条数
            $lists = Db::name('topic_message')->alias('a')
                ->where($map)
                ->join('up_u_user uuu','uuu.user_id = a.topic_message_user','left')
                ->join('up_topic ut','ut.topic_id =a.topic_id','left')
                ->page($NowPage,$limits)
                ->field('a.*,uuu.user_username,uuu.user_phone,ut.topic_title')
                ->order('a.topic_message_time desc')
                ->select();

            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    /***
     * @return \think\response\Json
     * 上架
     */
    public function up(){
        $task_id = input('param.id');
        $res = Db::name('topic_message')->where('topic_message_id',$task_id)->setField('topic_status',1);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }

    /***
     * @return \think\response\Json
     * 下架
     */
    public function down(){
        $task_id = input('param.id');
        $res = Db::name('topic_message')->where('topic_message_id',$task_id)->setField('topic_status',2);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }

    public function del(){
        $id = input('param.id');
        Db::table('up_topic_message')->where('topic_message_id',$id)->delete();
        //查询这个留言回复的数量
        $count1 = Db::table('up_topic_reply')->where('reply_message_id','IN',$id)->count();
        if($count1>0){
            Db::table('up_topic_reply')->where('reply_message_id','IN',$id)->setField('reply_status',2);
        }
        return json(['code'=>200,'msg'=>'删除成功']);
    }

}